Shader "Unlit/HpBar"
{
    Properties
    {
        [NoScaleOffset]_MainTex ("Texture", 2D) = "white" {}
        _Health ("Health",Range(0,1)) = 0.5
        _BorderSize("Border Size",Range(0,0.5)) = 0.2
        _HeightSize("Height Size",Range(0.5,50)) = 8
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue" = "Transparent" }

        Pass
        {
            ZWrite Off
            //src * srcAlpha + dst * (1-srcAplha)
            Blend SrcAlpha OneMinusSrcAlpha //alpha blending
                        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
           
            #include "UnityCG.cginc"

            struct MeshData
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct Interpolators
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float _Health;
            float _BorderSize;
            float _HeightSize;
            
            Interpolators vert (MeshData v)
            {
                Interpolators o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            //这个函数的作用是，将本来是0~1的转换成，最小0.2，最大0.8  也就是小于0.2的都是0，大于0.8的都是0.8
            float InverseLerp(float a,float b,float v)
            {
                return (v-a)/(b-a); // 比如现在是0.2    0  / 0.6  
            }

            //这里的saturate就是 Clamp01
            fixed4 frag (Interpolators i) : SV_Target
            {
                float2 coords = i.uv;
                coords.x *= _HeightSize;
                //x坐标的范围是0~1 乘以八就是0~8
                //clamp之后得到的对比点  范围是 0.5~7.5 也就是说0~0.5的点对比点都是0.5   7.5~8的点对比点都是7.5
                float2 pointOnLineSeg = float2(clamp(coords.x,0.5,_HeightSize - 0.5),0.5);
                //distance(coords,pointOnLineSeg) 算出当前像素点到对比点的距离 范围是0~0.5
                //乘以二之后是0~1  减去1之后是-1~0 是为了得到范围外的像素点， -sdf小于0的是在范围外
                //那么胶囊体之外的点，就是大于1的点 比如1.1  那么减去1之后就是 0.1  之内的点就是小于1的点减去1就是 -0.1
                //所以clip要取反的，之外的点要小于0 就要加负号
                float sdf = distance(coords,pointOnLineSeg) * 2 - 1;
                //将范围之外的像素点全部clip掉
                clip(-sdf);
                float border = sdf + _BorderSize;
                float colorMask = step(0,-border);
                float healthbarMask = _Health > i.uv.x;//大于就是 1,1,1,1  小于等于就是0,0,0,0
                float3 healthBarColor = tex2D(_MainTex,float2(_Health,i.uv.y));
                if (_Health < 0.3)
                {
                    float flash = cos(_Time.y * 4) * 0.4 + 1;
                    healthBarColor *= flash;
                }
                float4 finalColor = float4(healthBarColor * healthbarMask * colorMask,healthbarMask);
                if (colorMask <= 0)
                {
                    finalColor = float4(0,1,0,1);
                }
                return finalColor;
            }
            ENDCG
        }
    }
}
